<template>
    <div style="height:100%;">
      <div>
        <div class='content-title'>
          <div class='content-title-parent'>失物管理</div>
          /
          <div class='content-title-child'>分类管理</div>
        </div>
        <hr class='content-hr'/>
      </div>
      <!--增加按钮-->
      <el-row class='lostCategoryClass'>
        <el-button type="primary" style="float:right;margin-right:20px" @click="addVisable = true">添加</el-button>
      </el-row>
      <!--表格数据-->
      <el-row style="margin-top:10px">
        <el-table 
          v-loading="loading"
          :element-loading-text="loadingText"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255, 255, 255, 0.8)"
          :data="filterFormFro.tableData" 
          :border="true"
          :row-class-name="tableRowClassName"
          :header-cell-class-name = "tableHeaderClassName"
          v-bind:style="{width: tableWidth + 'px'}">
          <el-table-column prop="index" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="className" label="分类名称" sortable width="300">
          </el-table-column>
          <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 表格下方分页 -->
      <el-row>
        <div class="pagination" style="text-align:center" v-show="paginationVisible">
          <el-pagination
            @current-change="handleCurrentChange"
            background
            :current-page="filterForm.pageNum"
            :page-size="15"
            layout="total, prev, pager, next"
            :page-count="filterFormFro.allPages">
          </el-pagination>
        </div>
      </el-row>
      <!-- 添加分类信息弹窗 -->
      <el-dialog title="增加失物分类:"   :visible.sync="addVisable"
          :closeOnClickModal="false" 
          width="500px"
          class="categoryManaDialog"
          :close-on-click-modal="false" 
          :close-on-press-escape="false"
          @close="resetForm('addForm')">
        <el-form :model="addForm" :rules="addRules" ref="addForm">
            <el-form-item label="分类名称：" :label-width="formLabelWidth" prop="className">
              <el-input style='width:240px' @keyup.native="addForm.className=addForm.className.replace(/[^\w\u4e00-\u9fa5\s]/g, '')" clearable v-model.trim="addForm.className"></el-input>
            </el-form-item>
            <el-form-item label="" :label-width="formLabelWidth" >
              <el-button type="primary" style="width:240px" @click="submitAdd()">保 存</el-button>
            </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 编辑分类信息弹窗 -->
      <el-dialog title="编辑失物分类信息:"   :visible.sync="editVisable"
          :closeOnClickModal="false" 
          width="500px"
          class="categoryManaDialog"
          :close-on-click-modal="false" 
          :close-on-press-escape="false"
          @close="resetForm('editForm')">
        <el-form :model='editForm' ref="editForm" :rules="editRules">
            <el-form-item label="序号：" :label-width="formLabelWidth" prop="name">
              <label >{{editForm.index}}</label>
            </el-form-item>
            <el-form-item label="分类名称：" :label-width="formLabelWidth" prop="className">
              <el-input style='width:240px'  @keyup.native="editForm.className=editForm.className.replace(/[^\w\u4e00-\u9fa5\s]/g, '')" clearable v-model.trim="editForm.className"></el-input>
            </el-form-item>
            <el-form-item label="" :label-width="formLabelWidth" >
              <el-button type="primary" style="width:240px" @click="submitEdit()">保 存</el-button>
            </el-form-item>
        </el-form>
      </el-dialog>
    </div>
</template>
<style>
  .el-table .headerClass {
    background: #f0f9eb;
    text-align: center
  }
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
<script>
import apiBus from '../../api/businessPlat/apiBus.js'
import '../../../static/css/businessPlat/common.css'
import '../../../static/css/businessPlat/lostCategory.css'
export default {
  created () {
    this.getClassManaListFro()
  },
  data () {
    return {
      tableWidth: '',
      loading: true,
      loadingText: '正在拼命加载中，请稍等......',
      paginationVisible: false, // 表格下方分页是否显示
      noteVisible: false,
      formLabelWidth: '130px',
      addForm: {
        className: ''
      },
      addVisable: false,
      filterFormFro: {
        tableData: [],
        allPages: 0
      },
      filterForm: {
        pageNum: 1
      },
      editVisable: false,
      editForm: {
        index: '',
        className: '',
        classId: ''
      },
      deleteForm: {
        index: '',
        className: ''
      },
      addRules: {
        className: [
          { required: true, message: '请填写失物分类名称', trigger: 'blur' },
          { max: 8, message: '失物分类名称最多8个字', trigger: 'blur' }
        ]
      },
      editRules: {
        className: [
          { required: true, message: '请填写失物分类名称', trigger: 'blur' },
          { max: 8, message: '失物分类名称最多8个字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 增加分类，点击“保存”
    submitAdd () {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.addLostCategoryListFro()
        } else {
          return false
        }
      })
    },
    // 编辑分类，点击“保存”
    submitEdit () {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.editLostCategoryListFro()
        } else {
          return false
        }
      })
    },
    // 重置表单
    resetForm (formName) {
      this.$nextTick(function () {
        this.$refs[formName].resetFields()
      })
    },
    // 分页页面跳转时
    handleCurrentChange (val) {
      this.filterForm.pageNum = val
      this.getClassManaListFro()
    },
    setEditFro () {
      this.editVisable = false
      this.$message.success('分类' + this.editForm.className + '编辑成功')
    },
    tableHeaderClassName ({row, column, rowIndex, columnIndex}) {
      if (rowIndex === 0) {
        return 'headerClass'
      }
    },
    tableRowClassName ({row, rowIndex}) {
      return 'rowClass'
    },
    // 点击表格中的编辑按钮
    handleEdit (index, row) {
      this.editVisable = true
      this.editForm.index = row.index
      this.editForm.className = row.className.trim()
      this.editForm.classId = row.classId
    },
    // 点击表格中的删除按钮
    handleDelete (index, row) {
      this.deleteForm.index = row.index
      this.deleteForm.className = row.className
      this.deleteForm.classId = row.classId
      this.$confirm('确认删除该分类吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteLostCategoryListFro()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cancleEdit (formName) {
      this.resetError(formName)
      this.editVisable = false
    },
    // 重置表单
    resetError (formName) {
      this.$nextTick(function () {
        this.$refs[formName].resetFields()
      })
    },
    setDeleteFro () {
      this.noteVisible = false
      this.$message.success('分类' + this.deleteForm.className + '已删除')
    },
    /** 向后台请求 */
    // 查询所有失物分类列表
    getClassManaListFro () {
      this.loading = true
      this.filterFormFro.tableData = []
      let para = {
        header: JSON.stringify({
          'token': sessionStorage.getItem('token'),
          'beanId': 'lostMeterialService_v1'
        }),
        body: JSON.stringify({
          'pageNum': parseInt(this.filterForm.pageNum)
        })
      }
      apiBus.getClassManaList(para).then(res => {
        this.loading = false
        if (res.data.body.result === 1000) {
          if (res.data.body.content.list.length > 0) {
            this.$message.success('成功提取失物分类信息！')
            let resData = res.data.body.content.list
            this.filterFormFro.tableData = resData
            this.filterFormFro.allPages = res.data.body.content.allPages
            for (let i = 0; i < resData.length; i++) {
              this.filterFormFro.tableData[i].index = (parseInt(this.filterForm.pageNum) - 1) * 15 + i + 1
            }
            this.filterFormFro.allPages > 1 ? this.paginationVisible = true : this.paginationVisible = false
          } else {
            this.paginationVisible = false
            this.$message.warning('没有相关失物分类信息')
          }
        } else {
          this.paginationVisible = false
          this.$message.error(res.data.body.resultDesp)
        }
      })
      .catch(error => {
        this.paginationVisible = false
        this.loading = false
        this.$message.error(error)
      })
    },
    // 增加失物分类
    addLostCategoryListFro () {
      let para = {
        header: JSON.stringify({
          'token': sessionStorage.getItem('token'),
          'beanId': 'lostMeterialService_v1'
        }),
        body: JSON.stringify({
          'className': this.addForm.className
        })
      }
      apiBus.addLostCategoryList(para).then(res => {
        this.loading = false
        if (res.data.body.result === 1000) {
          this.$message.success('成功添加失物分类信息！')
          this.addVisable = false
          this.filterForm.pageNum = 1
          this.getClassManaListFro()
        } else {
          this.$message.error(res.data.body.resultDesp)
        }
      })
      .catch(error => {
        this.$message.error(error)
      })
    },
    // 删除失物分类
    deleteLostCategoryListFro () {
      let para = {
        header: JSON.stringify({
          'token': sessionStorage.getItem('token'),
          'beanId': 'lostMeterialService_v1'
        }),
        body: JSON.stringify({
          'classId': parseInt(this.deleteForm.classId)
        })
      }
      apiBus.deleteLostCategoryList(para).then(res => {
        this.loading = false
        if (res.data.body.result === 1000) {
          this.$message.success('成功删除失物分类信息')
          this.filterForm.pageNum = 1
          this.getClassManaListFro()
        } else {
          this.$message.error(res.data.body.resultDesp)
        }
      })
      .catch(error => {
        this.loading = false
        this.$message.error(error)
      })
    },
    // 编辑失物分类
    editLostCategoryListFro () {
      let para = {
        header: JSON.stringify({
          'token': sessionStorage.getItem('token'),
          'beanId': 'lostMeterialService_v1'
        }),
        body: JSON.stringify({
          'classId': parseInt(this.editForm.classId),
          'className': this.editForm.className
        })
      }
      apiBus.editLostCategoryList(para).then(res => {
        this.loading = false
        if (res.data.body.result === 1000) {
          this.$message.success('成功编辑失物分类信息')
          this.editVisable = false
          this.filterForm.pageNum = 1
          this.getClassManaListFro()
        } else {
          this.$message.error(res.data.body.resultDesp)
        }
      })
      .catch(error => {
        this.loading = false
        this.$message.error(error)
      })
    }
  },
  mounted () {
    this.tableWidth = document.body.scrollWidth - 259 - 20
  }
}
</script>